<?php
/**
 * User: luchao
 * Createtime: 2016/10/24 13:58
 * desc：变更单
 */
namespace AuctionBack\Controller;

use AuctionCore\System\AuctionException;
use AuctionCore\ChangeOrder\ChangeOrder;
use AuctionCore\Cashier\Collection;
use AuctionCore\Cashier\Payment;
use Think\Log;

class ChangeOrderController extends BaseController
{
    public function __construct()
    {
        parent::__construct();
        $this->emp_id = $this->user_info['emp_id'];
    }

    public function index()
    {
        $this->display();
    }

    //列表
    public function getList()
    {
        $page = I("get.p", 1);
        $size = 10;
        $params = array();
        $params['status'] = I("get.status/d");
        $params['order_no'] = trim(I("get.order_no"));
        $params['sno'] = trim(I("get.sno"));
        $params['modify_no'] = trim(I("get.modify_no"));
        $params['car_plate_number'] = trim(I("get.car_plate_number"));
        $location_area = I('get.location_area', 0);
        if (!$location_area) {
            $location_area = $this->user_info['bc_code'];
        } else {
            $location_area = [$location_area];
        }
        $params['location_area'] = $location_area;
        $modify_reason = C('MODIFY_REASON');
        //获取list
        $result = ChangeOrder::ChangeOrderList($page, $size, $params);
        // dump($result);
        $Page = new \AuctionCore\Common\AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show(); // 分页显示输出
        //模板赋值
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('list', $result['list']);
        $this->assign("params", $params);
        $this->assign("modify_reason", $modify_reason);
        $this->display();
    }

    //处理详情页
    public function deal()
    {
        $mid = I('get.mid');
        if ($mid < 1) {
            $this->helpJsonResult(500, '变更单id不能为空');
        }
        $changeorder = new ChangeOrder($mid);
        $changeInfo = $changeorder->detail();
        if ($changeInfo['status'] > 0) {
            $this->helpJsonResult(500, '变更单数据已处理');
        }
        $co = new ChangeOrder($mid);
        $res = $co->ChangeOrderDetail();
        $this->assign('res', $res);
        $this->display();
    }

    //详情页
    public function detail()
    {
        $mid = I('get.mid');
        if ($mid < 1) {
            $this->helpJsonResult(500, '变更单id不能为空');
        }
        $co = new ChangeOrder($mid);
        $res = $co->ChangeOrderDetail();
        $modify_reason = C('MODIFY_REASON');
        $modify_status = C('MODIFY_STATUS');
        $this->assign('res', $res);
        $this->assign('modify_reason', $modify_reason);
        $this->assign('modify_status', $modify_status);
        $this->display();
    }


    //详情页
    public function voidDetail()
    {
        $mid = I('get.mid');
        if ($mid < 1) {
            $this->helpJsonResult(500, '变更单id不能为空');
        }
        $co = new ChangeOrder($mid);
        $res = $co->ChangeOrderDetail();
        $res['is_valid'] = 1;
        $modify_reason = C('MODIFY_REASON');
        $modify_status = C('MODIFY_STATUS');
        $this->assign('res', $res);
        $this->assign('modify_reason', $modify_reason);
        $this->assign('modify_status', $modify_status);
        $this->display('ChangeOrder/detail');
    }

    //车商处理
    public function dealerDeal()
    {
        $mid = I('get.mid');
        if ($mid < 1) {
            $this->helpJsonResult(500, '变更单id不能为空');
        }
        $changeorder = new ChangeOrder($mid);
        $changeInfo = $changeorder->detail();
        if ($changeInfo['status'] > 0) {
            $this->helpJsonResult(500, '变更单数据已处理');
        }
        $modify_reason = I('post.modify_reason');
        $modify_result = I('post.modify_result');
        $modify_desc = I('post.modify_desc');
        $payment_amount = I('post.payment_amount/d',0); 
        $account_name = I('post.account_name');
        $bank_account = I('post.bank_account');
        $bank_name = I('post.bank_name');
        $emp_id = $this->emp_id;
        $data = [];
        $data['modify_reason'] =$modify_reason;
        $data['modify_result'] =$modify_result;
        $data['modify_desc'] =$modify_desc;
        $data['violate_amount'] =$payment_amount;
        $data['dealer_deduction'] = $payment_amount;
        $data['account_name'] =$account_name;
        $data['bank_account'] =$bank_account;
        $data['bank_name'] =$bank_name;
        $data['emp_id'] =$emp_id;
        $res = $changeorder->dealerDeal($data);
        if (!$res) {
            $this->helpJsonResult(400, '处理失败');
        } else {
            $this->helpJsonResult(200, '处理成功');
        }

    }

    //车主处理
    public function ownerDeal()
    {
        $mid = I('get.mid');
        if ($mid < 1) {
            $this->helpJsonResult(500, '变更单id不能为空');
        }
        $changeorder = new ChangeOrder($mid);
        $changeInfo = $changeorder->detail();
        if ($changeInfo['status'] > 0) {
            $this->helpJsonResult(500, '变更单数据已处理');
        }
        $modify_reason = I('post.modify_reason');
        $modify_result = I('post.modify_result');
        $modify_desc = I('post.modify_desc');
        $payment_amount = I('post.payment_amount/d',0);
        $collection_amount = I('post.owner_payment_amount/d',0);
        $account_name = I('post.account_name');
        $bank_account = I('post.bank_account');
        $bank_name = I('post.bank_name');
        $emp_id = $this->emp_id;
        $data = [];
        $data['modify_reason'] =$modify_reason;
        $data['modify_result'] =$modify_result;
        $data['modify_desc'] =$modify_desc;
        $data['payment_amount'] =$payment_amount;
        $data['dealer_compensate'] = $payment_amount;

        $data['collection_amount'] =$collection_amount;
        $data['owner_deduction'] = $collection_amount;
        $data['account_name'] =$account_name;
        $data['bank_account'] =$bank_account;
        $data['bank_name'] =$bank_name;
        $data['emp_id'] =$emp_id;
        $res = $changeorder->ownerDeal($data);
        if (!$res) {
            $this->helpJsonResult(400, '处理失败');
        } else {
            $this->helpJsonResult(200, '处理成功');
        }
    }

    //作废
    public function modifylist_cancel()
    {
        $mid = I('post.mid');
        if ($mid < 1) {
            $this->helpJsonResult(500, '变更单id不能为空');
        }
        $changeorder = new ChangeOrder($mid);
        $changeInfo = $changeorder->detail();
        if ($changeInfo['status'] == 4) {
            $this->helpJsonResult(500, '变更单数据已作废');
        }
        $data = [];
        $data['emp_id'] = $this->emp_id;
        $res = $changeorder->modifylist_cancel($data);
        if (!$res) {
            $this->helpJsonResult(400, '处理失败');
        } else {
            $this->helpJsonResult(200, '处理成功');
        }
    }

    //拒绝执行
    public function refuse_execute()
    {
        $mid = I('post.mid');
        if ($mid < 1) {
            $this->helpJsonResult(500, '变更单id不能为空');
        }
        $changeorder = new ChangeOrder($mid);
        $res = $changeorder->refuse_execute($this->emp_id);
        if (!$res) {
            $this->helpJsonResult(400, '处理失败');
        } else {
            $this->helpJsonResult(200, '处理成功');
        }
    }

    //再次变更详情页
    public function again_change_detail()
    {
        $mid = I('get.mid');
        if ($mid < 1) {
            $this->helpJsonResult(500, '变更单id不能为空');
        }
        $changeorder = new ChangeOrder($mid);
        $res = $changeorder->again_change_detail();
        $modify_reason = C('MODIFY_REASON');
        $modify_status = C('MODIFY_STATUS');
        $this->assign('res', $res);
        $this->assign('modify_reason', $modify_reason);
        $this->assign('modify_status', $modify_status);
        $this->display();
    }

    // //再次变更开始 锁定相关表
    // public function againChange()
    // {
    //     $changeId = I('post.change_id');
    //     if ($changeId < 1) {
    //         $this->helpJsonResult(500, '变更单id不能为空');
    //     }
    //     $res = ChangeOrder::lockAll($changeId);
    //     if (!$res) {
    //         $this->helpJsonResult(400, '处理失败');
    //     } else {
    //         $this->helpJsonResult(200, '处理成功');
    //     }
    // }

    // //取消变更 解锁相关的表
    // public function cancelAgainChange()
    // {
    //     $changeId = I('get.change_id');
    //     if ($changeId < 1) {
    //         $this->helpJsonResult(500, '变更单id不能为空');
    //     }
    //     $res = ChangeOrder::unLockAll($changeId);
    //     if (!$res) {
    //         $this->helpJsonResult(400, '处理失败');
    //     } else {
    //         $this->helpJsonResult(200, '处理成功');
    //     }
    // }

    private function check_again_change_deal($mid)
    {
        if ($mid < 1) {
            $this->helpJsonResult(500, '变更单id不能为空');
        };
        $changeorder = new ChangeOrder($mid);
        $changeOrderInfo = $changeorder->detail();
        if ($changeOrderInfo['source'] != 7 && $changeOrderInfo['source'] != 8 && $changeOrderInfo['modify_result'] != 1) {
            $this->helpJsonResult(500, '该变更单不能再次变更');
        }
    }

    //车商车主赔付
    public function dealerOwnerPayment()
    {
        $mid = I('get.mid');
        $this->check_again_change_deal($mid);
        $modify_reason = I('post.modify_reason');
        $modify_result = I('post.modify_result');
        $modify_desc = I('post.modify_desc');
        $account_name = I('post.account_name');
        $bank_account = I('post.bank_account');
        $bank_name = I('post.bank_name');
        $od_type = I('post.od_type');
        $payment_amount = I('post.payment_money');
        $data = [];
        $data['modify_reason'] = $modify_reason;
        $data['modify_result'] = $modify_result;
        $data['modify_desc'] = $modify_desc;
        $data['account_name'] = $account_name;
        $data['bank_account'] = $bank_account;
        $data['bank_name'] = $bank_name;
        $data['originator'] = $this->emp_id;
        $data['payment_amount'] = $payment_amount;
        if ($od_type == 1) {
            $data['dealer_compensate'] = $payment_amount;
        } else {
            $data['owner_compensate'] = $payment_amount;
        }
        $res = ChangeOrder::dealerOwnerPayment($data);
        if (!$res) {
            $this->helpJsonResult(400, '处理失败');
        } else {
            $this->helpJsonResult(200, '处理成功');
        }
    }

    //车商 检测失误 ->订单关闭
    public function dealerPaymentColse()
    {
        $mid = I('get.mid');
        $this->check_again_change_deal($mid);
        $modify_reason = I('post.modify_reason');
        $modify_result = I('post.modify_result');
        $modify_desc = I('post.modify_desc');
        $account_name = I('post.account_name');
        $bank_account = I('post.bank_account');
        $bank_name = I('post.bank_name');
        $od_type = I('post.od_type');
        $payment_amount = I('post.payment_money');
        $data = [];
        $data['modify_reason'] = $modify_reason;
        $data['modify_result'] = $modify_result;
        $data['modify_desc'] = $modify_desc;
        $data['account_name'] = $account_name;
        $data['bank_account'] = $bank_account;
        $data['bank_name'] = $bank_name;
        $data['originator'] = $this->emp_id;
        $data['payment_amount'] = $payment_amount;
        $data['dealer_compensate'] = $payment_amount;
        $res = ChangeOrder::dealerPaymentColse($data);
        if (!$res) {
            $this->helpJsonResult(400, '处理失败');
        } else {
            $this->helpJsonResult(200, '处理成功');
        }
    }

    //车主争议 订单关闭
    public function ownerPaymentColse()
    {
        $mid = I('get.mid');
        $this->check_again_change_deal($mid);
        $modify_reason = I('post.modify_reason');
        $modify_result = I('post.modify_result');
        $modify_desc = I('post.modify_desc');
        $account_name = I('post.account_name');
        $bank_account = I('post.bank_account');
        $bank_name = I('post.bank_name');
        $od_type = I('post.od_type');
        $payment_amount = I('post.payment_money');
        $owner_payment_money = I('post.owner_payment_money');
        $data = [];
        $data['modify_reason'] = $modify_reason;
        $data['modify_result'] = $modify_result;
        $data['modify_desc'] = $modify_desc;
        $data['account_name'] = $account_name;
        $data['bank_account'] = $bank_account;
        $data['bank_name'] = $bank_name;
        $data['originator'] = $this->emp_id;
        $data['payment_amount'] = $payment_amount;
        $data['owner_payment_money'] = $owner_payment_money;
        $data['dealer_compensate'] = $payment_amount;
        $data['owner_compensate'] = $owner_payment_money;

        $res = ChangeOrder::ownerPaymentColse($data);
        if (!$res) {
            $this->helpJsonResult(400, '处理失败');
        } else {
            $this->helpJsonResult(200, '处理成功');
        }
    }

    //再次变更->车商违约
    public function dealerViolate()
    {
        $mid = I('get.mid');
        $this->check_again_change_deal($mid);
        $modify_reason = I('post.modify_reason');
        $modify_result = I('post.modify_result');
        $modify_desc = I('post.modify_desc');
        $account_name = I('post.account_name');
        $bank_account = I('post.bank_account');
        $bank_name = I('post.bank_name');
        $od_type = I('post.od_type');
        $payment_amount = I('post.payment_money');
        $data = [];
        $data['modify_reason'] = $modify_reason;
        $data['modify_result'] = $modify_result;
        $data['modify_desc'] = $modify_desc;
        $data['account_name'] = $account_name;
        $data['bank_account'] = $bank_account;
        $data['bank_name'] = $bank_name;
        $data['originator'] = $this->emp_id;
        $data['payment_amount'] = $payment_amount;
        $data['dealer_deduction'] = $payment_amount;
        $res = ChangeOrder::dealerViolate($data);
        if (!$res) {
            $this->helpJsonResult(400, '处理失败');
        } else {
            $this->helpJsonResult(200, '处理成功');
        }
    }

    public function ownerViolate()
    {
        $mid = I('get.mid');
        $this->check_again_change_deal($mid);
        $modify_reason = I('post.modify_reason');
        $modify_result = I('post.modify_result');
        $modify_desc = I('post.modify_desc');
        $account_name = I('post.account_name');
        $bank_account = I('post.bank_account');
        $bank_name = I('post.bank_name');
        $od_type = I('post.od_type');
        $payment_amount = I('post.payment_money');
        $owner_payment_money = I('post.owner_payment_money');
        $data = [];
        $data['modify_reason'] = $modify_reason;
        $data['modify_result'] = $modify_result;
        $data['modify_desc'] = $modify_desc;
        $data['account_name'] = $account_name;
        $data['bank_account'] = $bank_account;
        $data['bank_name'] = $bank_name;
        $data['originator'] = $this->emp_id;
        $data['payment_amount'] = $payment_amount;
        $data['owner_payment_money'] = $owner_payment_money;
        $data['dealer_compensate'] = $payment_amount;
        $data['owner_deduction'] = $owner_payment_money;
        if ($od_type == 1) {
            $data['dealer_compensate'] = $payment_amount;
        } else {
            $data['owner_compensate'] = $payment_amount;
        }
        $res = ChangeOrder::ownerViolate($data);
        if (!$res) {
            $this->helpJsonResult(400, '处理失败');
        } else {
            $this->helpJsonResult(200, '处理成功');
        }
    }

}